package com.pureut.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.order.domain.CustomerComplaintsManagement;
import com.pureut.order.domain.CustomerComplaintsSolution;
import com.pureut.order.mapper.CustomerComplaintsSolutionMapper;
import com.pureut.order.service.CustomerComplaintsManagementService;
import com.pureut.order.service.CustomerComplaintsSolutionService;
import com.pureut.order.vo.CustomerComplaintsSolutionVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/***
 *
 * Author:C
 * Date:2022/11/30 14:06
 * @DESC
 */
@Service
public class CustomerComplaintsSolutionServiceImpl extends ServiceImpl<CustomerComplaintsSolutionMapper, CustomerComplaintsSolution> implements CustomerComplaintsSolutionService {

    @Resource
    CustomerComplaintsManagementService customerComplaintsManagementService;

    /**
     * 上传解决方案
     *
     * @param customerComplaintsSolutionVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean upload(CustomerComplaintsSolutionVo customerComplaintsSolutionVo) {
        CustomerComplaintsManagement customerComplaintsManagement = customerComplaintsManagementService.getById(customerComplaintsSolutionVo.getCustomerComplaintsId());
        if (4 == customerComplaintsManagement.getCustomerReviewStatus()) {
            CustomerComplaintsSolution customerComplaintsSolution = new CustomerComplaintsSolution();
            customerComplaintsSolution.setSolution(Integer.parseInt(customerComplaintsSolutionVo.getSolution()));
            customerComplaintsSolution.setCreateBy(SecurityUtils.getUsername());
            customerComplaintsSolution.setCreateTime(new Date());
            customerComplaintsSolution.setCustomerComplaintsId(customerComplaintsSolutionVo.getCustomerComplaintsId());
            customerComplaintsSolution.setSolutionUrl(customerComplaintsSolutionVo.getSolutionUrl());
            customerComplaintsSolution.setSolutionRemark(customerComplaintsSolutionVo.getSolutionRemark());
            boolean b = save(customerComplaintsSolution);
            if (b) {
                customerComplaintsManagement.setCustomerReviewStatus(5);
                return customerComplaintsManagementService.updateById(customerComplaintsManagement);
            } else {
                throw new GlobalException("上传失败");
            }
        } else {
            throw new GlobalException("不符合上传条件");
        }

    }
}
